package sanrenxing.dayEnd.backRepertoryTotal.dao;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import sanrenxing.dayEnd.backRepertoryTotal.domain.SecuritiesStock;
import sanrenxing.dayEnd.backRepertoryTotal.domain.SecuritiesStockTwo;

import java.util.List;
import java.util.Map;

/**
 * 功能：证券库存的数据层接口
 * 作者：赵思睿
 * 日期：2021/3/12 0012-14:13
 * 版本：1.0
 */
@Repository
public interface SecuritiesStockDao {
    /**
     * 将证券库存信息添加到数据库表中
     * @param securitiesStock 证券库存对象
     * @return 1：添加成功；0：添加失败
     */
    @Insert("insert into securitiesStock values(#{secId},#{seId},#{costs},#{secNum},#{sumMoney},to_date('${dates}','yyyy-MM-dd'),#{fundId},#{sign},#{caId})")
    public int add(SecuritiesStock securitiesStock);

    /**
     * 根据证券库存编号删除证券库存信息
     * @param secId 证券库存编号
     * @return 1：删除成功；0：删除失败
     */
    @Delete("delete from securitiesStock where secId=#{secId}")
    public int delete(@Param("secId") String secId);

    /**
     * 按条件进行分页查询
     * @param map 映射集合对象
     * @return 证券库存对象集合
     */
    @Select("<script>" +
            "select ss.secId,fs.seName,ss.costs,ss.secNum,ss.sumMoney,ss.dates,f.fundName,ss.sign,ca.accName,fs.seType as seType from " +
            "((select rownum rn,securitiesStock.* from securitiesStock" +
            "<where>#{end}>=rownum</where>) ss) join financialsecurities fs on ss.seId=fs.seId " +
            "join fundSetup f on ss.fundId=f.fundId join cashAccount ca on ss.caId=ca.caId" +
            "<where>" +
            "<if test='secStock.sign!=0'>" +
            " and sign=#{secStock.sign}" +
            "</if>" +
            "<if test='secStock.dates!=null'>" +
            " and to_char(dates,'yyyy-MM-dd')=#{secStock.dates}" +
            "</if>" +
            " and rn>=#{start}" +
            "</where>" +
            "</script>")
    public List<SecuritiesStockTwo> selectAll(Map<String, Object> map);

    /**
     * 按查询条件统计记录数
     * @return 统计行数
     */
    @Select("select count(*) as counts from securitiesStock")
    public int count();
}
